/*
 *
 * CardFileController.m
 *
 *	You may freely copy, distribute and reuse the code in this example.
 *	NeXT disclaims any warranty of any kind, expressed or implied, as to
 *	its fitness for any particular use.
 *
 */

#import "CardFileController.h"

@implementation CardFileController

- init
{
	[super init];
	customerList = [[List alloc] init];
	return self;
}

- awakeFromNib
{
	[self addCustomer:self];
	[[nameField window] makeKeyAndOrderFront:self];
	return self;
}

- free
{
	[customerList freeObjects];
	[customerList free];
	return [super free];
}

- addCustomer:sender
{
	currentCustomer = [[Customer alloc] init];
	[customerList addObject:currentCustomer];
	[customerBox setTitle:"Enter Customer Data"];
	[self updateInspector];
    return self;
}

- deleteCustomer:sender
{
	// XXX - probably should confirm this operation with the User
	if (!currentCustomer) return self; // XXX - better to disable the menu item
	
	[customerList removeObject:currentCustomer];
	[currentCustomer free];
	[self firstCustomer:self];
    return self;
}

- firstCustomer:sender
{
	currentCustomer = [customerList objectAt:0];
	if (!currentCustomer) {
		[self clearInspector];
		[[customerBox setTitle:"No Customers"] display]; 
		return self;
	}
	[customerBox setTitle:"First Customer"];
	[self updateInspector];
	return self;
}

- nextCustomer:sender
{
	id  nextCustomer;
	int customerIndex = [customerList indexOf:currentCustomer];
	
	if (customerIndex == NX_NOT_IN_LIST)
		return self;	// should warn user entry not found in list

	nextCustomer = [customerList objectAt:(customerIndex + 1)];
	if (!nextCustomer) {
		[[customerBox setTitle:"Last Customer"] display];
		NXBeep();
		return self;	// XXX - better to disable the Next button!
	}
	currentCustomer = nextCustomer;
	[customerBox setTitle:"Current Customer"];
	[self updateInspector];
    return self;
}

- prevCustomer:sender
{
	id  prevCustomer;
	int customerIndex = [customerList indexOf:currentCustomer];
	
	if (customerIndex == NX_NOT_IN_LIST)
		return self;	// should warn user entry on found in list
	if (customerIndex == 0) {
		[[customerBox setTitle:"First Customer"] display];
		NXBeep();
		return self;	// XXX - better to disable the Prev button!
	}
	
	prevCustomer = [customerList objectAt:(customerIndex - 1)];
	if (!prevCustomer) 
		return self;	// should also alert user that this is last
	currentCustomer = prevCustomer;
	[customerBox setTitle:"Current Customer"];
	[self updateInspector];
    return self;
}

- nameChanged:sender
{
	[currentCustomer setCustomerName:[sender stringValue]];
    return self;
}

- uidChanged:sender
{
	[currentCustomer setCustomerUid:[sender intValue]];
    return self;
}

- updateInspector
{
	if (!currentCustomer) return self;
	
	[nameField setStringValue:[currentCustomer customerName]];
	[uidField setIntValue:[currentCustomer customerUid]];
	[listCountField setIntValue:[customerList count]];
	[nameField selectText:self];
	[customerBox display];
	return self;
}

- clearInspector
{
	[nameField setStringValue: NULL];
	[uidField setStringValue: NULL];
	[listCountField setStringValue: NULL];
	
	[nameField selectText:self];
	return self;
}


@end
